package com.roc.androidlive.util;

import android.app.ActivityManager;
import android.content.Context;
import android.app.ActivityManager.RunningServiceInfo;
import android.util.Log;

import java.util.ArrayList;

public class ServiceUtils {
    /**
     * 判断服务是否开启
     *
     * @return boolean
     */
    public static boolean isServiceRunning(Context context, String ServiceName) {
        if (("").equals(ServiceName) || ServiceName == null)
            return false;
        ActivityManager myManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager
                .getRunningServices(Integer.MAX_VALUE);
        if (runningService != null) {
            for (int i = 0; i < runningService.size(); i++) {
                String name = runningService.get(i).service.getShortClassName();
                String fullName = runningService.get(i).service.getClassName();
                Log.d("ServiceUtils", "the service name is:" + fullName);
                Log.d("ServiceUtils", "the service name is:" + name);
                if (fullName.equals(ServiceName)) {
                    return true;
                }
            }
        }
        return false;
    }
}
